package  com.base.util
{
	import com.base.data.message.MessageBean;
	import com.base.properties.Properties;
	
	import flash.text.*;
	import flash.utils.*;
	
	/** 
	 * 用于打包multipart/form-data格式HTTP数据包的类 
	 */ 
	public class MultiPartFormData  
	{
		public static var Boundary:String= "---------------------------7db1851cd1158"; 
		
		private var nameFormat:String="Content-Disposition: form-data; name=\"XXXX\""; 
		
		private var fileFormat:String="Content-Disposition: form-data; name=\"XXXX\"; filename=\"XXXXXXXX\""; 
		
		private var fileContentType:String= "Content-Type: XXXX"; 
		
		private var byteArray:ByteArray;  
		
		public function MultiPartFormData () 
		{ 
			byteArray = new ByteArray(); 
		} 
		
		/** 
		 * 添加数据
		 */ 
		public function addData( name:String,  val:String):void 
		{ 
			var _name:String = nameFormat.replace("XXXX" , name ); 
			
			byteArray.writeMultiByte( "--"+Boundary+"\r\n","UTF-8"); 
			byteArray.writeMultiByte( _name+"\r\n\r\n","UTF-8"); 
			byteArray.writeMultiByte( val+"\r\n","UTF-8"); 
		} 
		
		/**
		 * 添加文件 
		 * @param name 属性名
		 * @param fileName 文件名
		 * @param byte 文件内容
		 * @param ContentType MimiType
		 * 
		 */		
		public function addFile( name:String, fileName:String,bytes:ByteArray, ContentType:String = "application/octet-stream" ):void 
		{ 
			var _name:String = fileFormat.replace("XXXX" , name ).replace("XXXXXXXX" , fileName );
			var _contentType:String = fileContentType.replace("XXXX",ContentType); 
			
			byteArray.writeMultiByte( "--"+Boundary+"\r\n" , "UTF-8" ); 
			byteArray.writeMultiByte( _name+"\r\n" , "UTF-8" ); 
			byteArray.writeMultiByte( _contentType+"\r\n\r\n" , "UTF-8" ); 
			
			byteArray.writeBytes( bytes , 0 , bytes.length ); 
			
			byteArray.writeMultiByte("\r\n","UTF-8"); 
		} 
		
		/**
		 * 
		 * @param message
		 */		
		public function addMessage( message:MessageBean , properties:Properties ):void {
			var attriNames:Array = properties.propertyNames;
			for each( var attriName:String in attriNames ) {
				var formKey:* =  properties.getProperty(attriName)
				var formValue:* = findVal( attriName , message , properties );
				if( formValue != null )
					addData( formKey , formValue );
			}
		}
		
		 
		private function findVal( attriName:String , message:* ,properties:Properties ):* {
			var val:*;
			var compoundVal:*;
			var objInfo:XML = describeType( message );
			
			for each(var q:XML in objInfo.accessor){
				
				var realAttriName:String = q.@name;
				
				if( Utils.isSimple( String(q.@type) )  &&  realAttriName == attriName ) {
					
					//找到message对应的属性名,
					val = properties.getProperty(attriName+message[ attriName ]);
					return val ? val : message[attriName];
					
				} else {
					
					val = findVal( attriName , message[ q.@name ] , properties );
					if( val !=  null ) return val;
				}
			}
			
			
			return null;
		}
		
		/** 
		 * 打包数据
		 */ 
		public function pack():void 
		{ 
			byteArray.writeMultiByte( "--"+Boundary+"--","UTF-8"); 
		} 
		
		/** 
		 * 获取数据,真正发送数据前先调用pack方法
		 */ 
		public function get data():ByteArray 
		{ 
			return byteArray; 
		} 
		
	} 
	
}